package pta;



import java.util.ArrayList;  

import java.util.Collections;  
import java.util.Scanner;  
		  
public class java8_8 {  
		  
	 public static void main(String[] args) {  
		        int n;  
		        Scanner in = new Scanner(System.in 

);  
		        n = in.nextInt();  
		        String[] strs = new String[100];  
		        String strs1;  
		        String[] strs2 = new String[100];  
		        String[] strs3 = new String[100];  
		        ArrayList<String> list = new ArrayList<String>();  
		        ArrayList<String> list1 = new ArrayList<String>();  
		        for (int i = 0; i < n; i++) {  
		            strs[i] = in.next();  
		        }  
		        for (int i = 0; i < n; i++) {  
		            strs2[i] = strs[i].substring(6, 10) + "-" + strs[i].substring(10, 12) + "-" + strs[i].substring(12, 14);  
		            list.add(strs2[i]);  
		  
		        }  
		        Collections.sort(list);  
		                  
		        for (int i = 0; i < n; i++) {  
		            strs3[i] = strs[i].substring(6, 14);  
		            list1.add(strs3[i]);  
		        }  
		        Collections.sort(list1);  
		        String [] temp=list1.toArray(new String[list1.size()]);  
		        
		        while (true) {  
		  
		            strs1 = in.next();  
		            if (strs1.equals("sort1")) {  
		  
		                for (String s : list) {  
		                    System.out.println(s);  
		                }  
		  
		                continue;  
		            }  
		            if (strs1.equals("sort2")) {  
		                for (int i = 0; i < n; i++) {  
		                    for (int j = 0; j < n; j++) {  
		                        if (strs[j].contains(temp[i])) {  
		                            System.out.println(strs[j]);  
		                            break;  
		                        }  
		                    }  
		  
		                }  
		                  
		                continue;  
		            }  
		            else {  
		                System.out.println("exit");  
		                break;  
		            }  
		  
		        }  
		  
		    }  
		  
	 
	}


